package com.jiliason.segmentation.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class CustomExclusionFilter extends AbstractMatcherFilter<ILoggingEvent> {

    private final Set<String> excludedLoggerNames = new HashSet<>();

    public void setExcludedLoggerNames(String commaSeparatedLoggerNames) {
        excludedLoggerNames.addAll(Arrays.asList(commaSeparatedLoggerNames.split(",")));
    }

    @Override
    public FilterReply decide(ILoggingEvent iLoggingEvent) {
        // 过滤掉包含自定义logger的日志
        if (excludedLoggerNames.contains(iLoggingEvent.getLoggerName())) {
            return FilterReply.DENY;
        }
        // 如果日志级别高于 INFO，则允许通过
        if (iLoggingEvent.getLevel().isGreaterOrEqual(Level.INFO)) {
            return FilterReply.NEUTRAL;
        }
        return FilterReply.DENY;
    }
}
